﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;

/// <summary>
/// 生成模型
/// </summary>
internal class CAutoModel
{
    /// <summary>
    /// 生成模型
    /// </summary>
    /// <param name="db_name"> 数据库名 </param>
    /// <param name="tablename"> 表名 </param>
    /// <param name="db"></param>
    /// <returns></returns>
    public static string CreateModel(string db_name, string tablename, CDatabase db)
    {
        string str_namespace = Cconst.DefaultNameSpace;         // 默认命名空间
        string auther = Cconst.DefaultAuther;                   // 默认作者

        return CreateModel(db_name, tablename, str_namespace, auther, db);
    }

    /// <summary>
    /// 生成模型
    /// </summary>
    /// <param name="db_name"> 数据库名 </param>
    /// <param name="tablename"> 表名 </param>
    /// <param name="str_namespace"> 命名空间 </param>
    /// <param name="db"></param>
    /// <returns></returns>
    public static string CreateModel(string db_name, string tablename, string str_namespace, string auther, CDatabase db)
    {
        if (str_namespace == "")
            str_namespace = Cconst.DefaultNameSpace;         // 默认命名空间

        if(auther == "")
            auther = Cconst.DefaultAuther;                   // 默认作者

        // 新建目录
        string fileName = CUtility.GetTableName(tablename);
        string directoryPath = CUtility.GetBaseDirectory() + db_name + "\\Model\\";
        bool exists = CFile.CreateDirectory(directoryPath);

        // 添加文件
        string filePath = CUtility.GetBaseDirectory() + db_name + "\\Model\\" + fileName + "Entity.cs";
        if (exists)
        {
            // 写文件内容
            StreamWriter w = new StreamWriter(filePath, true, Encoding.Default);

            w.WriteLine(@"/*======================================*\");
            w.WriteLine("* Author: " + auther);
            w.WriteLine("* LastModifyUser: " + auther);
            w.WriteLine("* Description: ");
            w.WriteLine("* FileName：" + fileName + "Entity");
            w.WriteLine("* CreateTime： " + DateTime.Now.ToString());
            w.WriteLine("* Remark: 模型");
            w.WriteLine(@"\*======================================*/");

            w.WriteLine("using System;");
            w.WriteLine("using System.Collections.Generic;");
            w.WriteLine("using System.Text;\r");
            w.WriteLine("namespace " + str_namespace + "\r{");
            w.WriteLine("    [Serializable]");
            w.WriteLine("    public class " + fileName + "Entity\r    {");
            w.WriteLine("        public " + fileName + "Entity(){ }\r");

            //w.WriteLine("        #region 公共属性\r");

            string sql = "sp_COLUMNS '" + tablename + "'";
            DataTable dt_columns = db.ExecuteSql(sql);

            string column_name = "";        // 字段名称
            string column_typename = "";    // varchar,int,datetime,float...
            string column_length = "";      // 长度
            string column_description = ""; // 说明
            string str_private = "";        // 记录私有属性
            string str_public = "";         // 公有属性

            for (int i = 0; i < dt_columns.Rows.Count; i++)
            {
                column_name = dt_columns.Rows[i]["column_name"].ToString();
                column_typename = dt_columns.Rows[i]["type_name"].ToString();
                column_length = dt_columns.Rows[i]["length"].ToString();

                // 查询出字段说明
                column_description = CUtility.GetColumnDescription(tablename, column_name, db);

                // 私有属性 
                str_private += "        /// <summary>\r";
                str_private += "        /// " + column_description + "\r";
                str_private += "        /// </summary>\r";
                str_private += "        private " + CUtility.SqlDataTypeToCodeType(column_typename) + " _" + column_name + ";\r\r";

                // 公有属性
                str_public += "        /// <summary>\r";
                str_public += "        /// " + column_description + "\r";
                str_public += "        /// </summary>\r";
                str_public += "        public " + CUtility.SqlDataTypeToCodeType(column_typename) + " " + CUtility.GetTableName(column_name) + " { get { return _" + column_name + "; } set { _" + column_name + " = value; }}\r\r";

                //w.WriteLine("       private " + SqlDataTypeToCodeType(column_typename) + " _" + column_name + ";\r");
                //w.WriteLine("       public " + SqlDataTypeToCodeType(column_typename) + " " + GetTableName(column_name) + " { get { return _" + column_name + "; } set { _" + column_name + " = value; }}\r");
            }
            w.WriteLine("        #region 私有属性\r");
            w.WriteLine(str_private);
            w.WriteLine("        #endregion\r");

            w.WriteLine("        #region 公共属性\r");
            w.WriteLine(str_public);
            w.WriteLine("        #endregion\r");

            w.WriteLine("   }");
            w.WriteLine("}");

            w.Flush();
            w.Close();
        }

        return tablename + "Entity.cs生成成功";
    }

}

